package com.azdebugit.java8.suanfa;


/**
 * Definition for singly-linked list.
 */
class SolutionA {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode first = head;
        ListNode last = head;
        for (int i=0;i<n;i++) {
            last = last.next;
        }
        while(last!=null && last.next!= null){
            last = last.next;
            first = first.next;
        }

        if (first == head && last == null) {
            return head.next;
        }
        ListNode thr = first.next;
        first.next = thr.next;
        thr.next = null;
        return head;
    }
}
